
package com.lzx.iteam.util;

import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.util.Log;
import android.widget.Toast;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.model.inner.GeoPoint;
import com.lzx.iteam.BaiduMapActivity;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

public class LocationUtil {

    public Context mContext;
    private LocationStatus mListener;
    private LocationClient mLocationClient;
    private long mLatitude;
    private long mLontitude;

    private BDLocationListener myListener = new MyLocationListener();

    public LocationUtil(Context context) {
        mContext = context;
        initLocation(-1);
    }

    public LocationUtil(Context context, int time) {
        mContext = context;
        initLocation(time);
    }

    public void setListener(LocationStatus listener) {
        mListener = listener;
    }

    public interface LocationStatus {
        public void onLocated(BDLocation location);
    }

    private void initLocation(int time) {
        if (time == -1) {
            time = 5000;
        }
        mLocationClient = new LocationClient(mContext);
        mLocationClient.registerLocationListener(myListener);
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true);
        option.setAddrType("all");//返回的定位结果包含地址信息
        option.setCoorType("bd09ll");//返回的定位结果是百度经纬度,默认值gcj02
        //  option.setScanSpan(time);//设置发起定位请求的间隔时间为10000ms
        option.disableCache(false);//禁止启用缓存定位
        mLocationClient.setLocOption(option);
        mLocationClient.start();
        requestLocation();
    }

    public void requestLocation() {
        if (mLocationClient != null && mLocationClient.isStarted()) {
            mLocationClient.requestLocation();
        }
    }


    public void resumeLocation() {
        if (mLocationClient != null) {
            mLocationClient.start();
        }
    }

    public void stopLocation() {
        if (mLocationClient != null) {
            mLocationClient.stop();
        }
    }

    public void destoryLocation() {
        if (mLocationClient != null) {
            mLocationClient.unRegisterLocationListener(myListener);
            mLocationClient.stop();
            mLocationClient = null;
        }
        mListener = null;
    }


    public class MyLocationListener implements BDLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {

            if (location == null) {
                return;
            }
           /* StringBuffer sb = new StringBuffer(256);
            sb.append("time : ");
            sb.append(location.getTime());
            sb.append("\nerror code : ");
            sb.append(location.getLocType());
            sb.append("\nlatitude : ");
            sb.append(location.getLatitude());
            sb.append("\nlontitude : ");
            sb.append(location.getLongitude());
            sb.append("\nradius : ");
            sb.append(location.getRadius());
            if (location.getLocType() == BDLocation.TypeGpsLocation){
                sb.append("\nspeed : ");
                sb.append(location.getSpeed());
                sb.append("\nsatellite : ");
                sb.append(location.getSatelliteNumber());
            } else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
                sb.append("\naddr : ");
                sb.append(location.getAddrStr());
            }*/
            if (mListener != null) {
                mListener.onLocated(location);
            }
        }

        public void onReceivePoi(BDLocation poiLocation) {
        }


    }


    public static LatLng getGeoPointBystr(String str, Context context) {
        LatLng latLng = null;
        if (str != null) {
            Geocoder gc = new Geocoder(context, Locale.CHINA);
            List<Address> addressList = null;
            try {
                addressList = gc.getFromLocationName(str, 1);
                if (!addressList.isEmpty()) {
                    Address address_temp = addressList.get(0);
                    //计算经纬度
                    /*double latitude = address_temp.getLatitude() * 1E6;
                    double longitude = address_temp.getLongitude() * 1E6; */
                    double latitude = address_temp.getLatitude() ;
                    double longitude = address_temp.getLongitude();
                    Log.d("获取经纬度:", "经度：" + latitude+",纬度：" + longitude);
                    latLng = new LatLng(latitude, longitude);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return latLng;
    }


}
